package com.heima.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.heima.article.dao.ApArticleConfigDao;
import com.heima.article.dao.ApArticleContentDao;
import com.heima.article.service.ApArticleFreemarkerService;
import com.heima.model.article.dao.ArticleDto;
import com.heima.model.article.dao.ArticleHomeDto;
import com.heima.model.article.entity.ApArticle;
import com.heima.article.dao.ApArticleDao;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.entity.ApArticleConfig;
import com.heima.model.article.entity.ApArticleContent;
import com.heima.model.article.entity.ArticleContants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVo;
import com.heima.model.wemedia.entity.KafkaMessageConstants;
import com.heima.utils.common.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章基础信息表(ApArticle)表服务实现类
 *
 * @author makejava
 * @since 2022-04-16 17:41:18
 */
@Service("apArticleService")
public class ApArticleServiceImpl implements ApArticleService {
    @Resource
    private ApArticleDao apArticleDao;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Resource
    private ApArticleConfigDao apArticleConfigDao;

    @Resource
    private ApArticleContentDao apArticleContentDao;

    @Resource
    private ApArticleFreemarkerService apArticleFreemarkerService;

    @Resource
    private KafkaTemplate kafkaTemplate;


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ApArticle queryById(Long id) {
        return this.apArticleDao.queryById(id);
    }



    /**
     * 新增数据
     *
     * @param apArticle 实例对象
     * @return 实例对象
     */
    @Override
    public ApArticle insert(ApArticle apArticle) {
        this.apArticleDao.insert(apArticle);
        return apArticle;
    }

    /**
     * 修改数据
     *
     * @param apArticle 实例对象
     * @return 实例对象
     */
    @Override
    public ApArticle update(ApArticle apArticle) {
        this.apArticleDao.update(apArticle);
        return this.queryById(apArticle.getId());
    }


    @Override
    public ResponseResult save(ArticleDto dto) {
        if(dto.getNewsId() == null || dto.getTitle() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

       ApArticle article= apArticleDao.queryByNewsId(dto.getNewsId());

       if(article == null){
           //新增
           apArticle.setId(snowflakeIdWorker.nextId());
           apArticle.setLikes(0);
           apArticle.setComment(0);
           apArticle.setCollection(0);
           apArticleDao.insert(apArticle);


           ApArticleConfig apArticleConfig = new ApArticleConfig();
           apArticleConfig.setArticleId(apArticle.getId());
           apArticleConfig.setIsComment(1);
           apArticleConfig.setIsDelete(0);
           apArticleConfig.setEnable(1);
           apArticleConfig.setIsForward(1);

           apArticleConfigDao.insert(apArticleConfig);

           ApArticleContent apArticleContent =new ApArticleContent();
           apArticleContent.setArticleId(apArticle.getId());
           apArticleContent.setContent(dto.getContent());

           apArticleContentDao.insert(apArticleContent);

       }else {
           //修改
           apArticle.setId(article.getId());
           apArticleDao.update(apArticle);

           ApArticleContent apArticleContent = apArticleContentDao.queryById(apArticle.getId());

           apArticleContent.setContent(dto.getContent());

           apArticleContentDao.update(apArticleContent);
       }

        String url = apArticleFreemarkerService.buildArticle2Minio(apArticle.getId(), dto.getContent());
       apArticle.setStaticUrl(url);
       apArticleDao.update(apArticle);

        //发送消息给MQ---->同步文章数给ES
        if (StringUtils.isEmpty(url)){
            throw new RuntimeException("生成文章静态详情页失败");
        }

        SearchArticleVo searchArticleVo = new SearchArticleVo();
        BeanUtils.copyProperties(apArticle, searchArticleVo);
        searchArticleVo.setContent(dto.getContent());
        searchArticleVo.setStaticUrl(url);

        kafkaTemplate.send(KafkaMessageConstants.AP_ARTICLE_ES_SYNC_TOPIC, JSON.toJSONString(searchArticleVo));
        return  ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult load(ArticleHomeDto dto, Short loadType) {
        if (dto.getSize()==null || dto.getSize() == 0) {
            dto.setSize(10);
        }
        dto.setSize(Math.min(dto.getSize(), 50));
        if(dto.getMaxBehotTime()==null){
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime()==null) {
            dto.setMinBehotTime(new Date());
        }
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleContants.DEFAULT_TAG);
        }
        Map<String, Object> queryParam = BeanUtil.beanToMap(dto);
        queryParam.put("loadType",loadType);

        List<ApArticle> articleList = apArticleDao.queryArticles(queryParam);

        return ResponseResult.okResult(articleList);
    }
}
